package com.zkthink.ceres.finance.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.finance.dao.MemberCashOutDetailMapper;
import com.zkthink.ceres.finance.dto.MemberCashOutAuditDTO;
import com.zkthink.ceres.finance.dto.MemberCashOutAuditPageDTO;
import com.zkthink.ceres.finance.dto.MemberCashOutDetailPageDTO;
import com.zkthink.ceres.finance.dto.MemberCashOutDetailSaveDTO;
import com.zkthink.ceres.finance.entity.MemberCashOutDetail;
import com.zkthink.ceres.finance.enums.AuditStatusEnum;
import com.zkthink.ceres.finance.enums.CashOutStatusEnum;
import com.zkthink.ceres.finance.service.MemberCashOutDetailService;
import com.zkthink.ceres.finance.vo.MemberCashOutDetailVO;
import com.zkthink.dozer.DozerUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 客户收款帐户表
 * </p>
 *
 * @author vivi.huang
 * @date 2020-05-25
 */
@Slf4j
@Service
@AllArgsConstructor
public class MemberCashOutDetailServiceImpl extends SuperServiceImpl<MemberCashOutDetailMapper, MemberCashOutDetail> implements MemberCashOutDetailService {

    @Autowired
    private MemberCashOutDetailMapper cashOutDetailMapper;
    private DozerUtils dozerUtils;

    /**
     * 提现
     *
     * @param collectingAccountSaveDTO
     * @return
     */
    @Override
    public R<Boolean> doMemberCashOut(MemberCashOutDetailSaveDTO collectingAccountSaveDTO) {
//        Boolean ifTiXian = memberCoinService.ifReleaseCoin(collectingAccountSaveDTO.getMid());
//        if(!ifTiXian){
//            return R.fail("考勤达标后方可提现");
//        }
//        MemberCashOutDetail detail = dozerUtils.map(collectingAccountSaveDTO, MemberCashOutDetail.class);
//        detail.setApplyTime(LocalDateTime.now());
//        detail.setAuditStatus(AuditStatusEnum.WAIT_TO_PROCESS.getCode());
//        detail.setCashOutStatus(CashOutStatusEnum.PROCESSING.getCode());
//
//        if (!checkEnoughBalance(detail.getAmount(), collectingAccountSaveDTO.getMid())) {
//            throw new BizException("余额不足");
//        }
        return R.success();
    }

    @Override
    public R<IPage<MemberCashOutDetailVO>> findMemberCashOutDetailPage(PageParams<MemberCashOutDetailPageDTO> pageParams) {
        IPage<MemberCashOutDetailVO> page = pageParams.getPage();
        queryCashOutDetail(pageParams, page);
        return R.success(page);
    }

    private void queryCashOutDetail(PageParams<MemberCashOutDetailPageDTO> params, IPage<MemberCashOutDetailVO> page) {
        MemberCashOutDetailPageDTO pageDTO = params.getModel();
        Integer total = this.cashOutDetailMapper.getTotalOfMemberCashOutDetailPage(pageDTO.getStartTime(), pageDTO.getEndTime());

        if (total > 0) {
            List<MemberCashOutDetailVO> cashOutDetailList = this.cashOutDetailMapper.findMemberCashOutDetailPage(
                    pageDTO.getStartTime(), pageDTO.getEndTime(), (page.getCurrent() - 1) * page.getSize(), page.getSize());
            page.setRecords(cashOutDetailList);
            page.setTotal(total);
        }
    }

    @Override
    public R<IPage<MemberCashOutAuditDTO>> findAuditMemberCashOutDetailPage(PageParams<MemberCashOutAuditPageDTO> pageParams) {
        IPage<MemberCashOutAuditDTO> page = pageParams.getPage();
        queryAuditCashOutDetail(pageParams, page);
        return R.success(page);
    }

    private void queryAuditCashOutDetail(PageParams<MemberCashOutAuditPageDTO> pageParams, IPage<MemberCashOutAuditDTO> page) {
        MemberCashOutAuditPageDTO pageDTO = pageParams.getModel();

        Integer total = this.cashOutDetailMapper.getTotalOfAuditMemberCashOutDetailPage(pageDTO.getNickname(),
                pageDTO.getMid(), pageDTO.getApplyTimeString(), pageDTO.getAuditStatus());

        if (total > 0) {
            List<MemberCashOutAuditDTO> cashOutAuditDTOList = this.cashOutDetailMapper.findAuditMemberCashOutDetailPage(
                    pageDTO.getNickname(), pageDTO.getMid(), pageDTO.getApplyTimeString(),
                    pageDTO.getAuditStatus(), (page.getCurrent() - 1) * page.getSize(), page.getSize());

            page.setTotal(total);
            page.setRecords(cashOutAuditDTOList);
        }
    }

    /**
     * 平台审核提现
     *
     * @param cashOutAuditDTO
     * @return
     */
    @Override
    public R<Boolean> auditMemberCashOut(MemberCashOutAuditDTO cashOutAuditDTO) {
        MemberCashOutDetail memberCashOutDetail = cashOutDetailMapper.selectById(cashOutAuditDTO.getId());
        memberCashOutDetail.setAuditStatus(AuditStatusEnum.PROCESSED.getCode());
        memberCashOutDetail.setCashOutStatus(cashOutAuditDTO.getCashOutStatus());
        memberCashOutDetail.setAuditTime(LocalDateTime.now());
        cashOutDetailMapper.updateById(memberCashOutDetail);

        if (cashOutAuditDTO.getAuditStatus() == CashOutStatusEnum.SUCCESS.getCode()) {
            //记录现金流
//            if (!saveCashHis(cashOutAuditDTO)) {
//                return R.fail("无法处理");
//            }
        }
        return R.success();
    }


    @Override
    public R<MemberCashOutAuditDTO> getMemberCashOutAudit(Long id) {
        MemberCashOutAuditDTO memberCashOutAudit = cashOutDetailMapper.getMemberCashOutAudit(id);
        return R.success(memberCashOutAudit);
    }


}
